from nd_utils.networkx_util import *

class Graph_similarity_matrix():
    def __init__(self, n_samples_dim_matrix ):
        self.similarity_matrix = n_samples_dim_matrix
        self.node_total_num = n_samples_dim_matrix.shape[0]
        self.similarity_inverse_dict = self._get_similarity_inverse_table()
        self.sorted_similarity_list = self._get_sorted_similarity_list()
        pass


    def _get_similarity_inverse_table(self):
        similarity_inverse_dict = {}
        for i in range( self.similarity_matrix.shape[0] -1 ):
            for j in range(i+1, self.similarity_matrix.shape[0] ):
                if self.similarity_matrix[i][j] != 0:
                    if self.similarity_matrix[i][j] not in similarity_inverse_dict:
                        similarity_inverse_dict[ self.similarity_matrix[i][j] ] = [ set([i, j]) ]
                    else:
                        similarity_inverse_dict[ self.similarity_matrix[i][j] ].append( set([i, j]) )
        return similarity_inverse_dict

    def _get_sorted_similarity_list(self):
        similarity_list = self.similarity_inverse_dict.keys()
        similarity_list.sort(reverse=True)
        return similarity_list

    def first_stage_graph_main(self,cluster_graph, threshold = 0):
        # cluster_graph = new_graph(self.node_total_num)

        for similarity in self.sorted_similarity_list:
            if similarity > threshold:
                pair_set_list = self.similarity_inverse_dict[ similarity ]
                cluster_graph = add_pair_set_to_graph(pair_set_list, cluster_graph ,_weight = 2.5)
        return cluster_graph




